﻿using System.Collections.Generic;
using System.ServiceModel;
using RestService.Models;

namespace RestService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single ,ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class GeoService : IGeoService
    {
        #region IGeoService Members

        private static readonly List<Vehicle> Vehicles = new List<Vehicle>();
        
        //Współrzędne graczy
        private static readonly List<newPlayerStatus> LocalizationPoints = new List<newPlayerStatus>(); 

        // komunikacja z bazą
        DataConnection dataConn = new DataConnection();
        GameLogic gameLogic = new GameLogic();

        public string XmlData(string id)
        {
            return "You requested product " + id;
        }

        public string JsonData(string id)
        {
            return "You requested product lalalalal" + id;
        }

        public IList<string> GetPlates()
        {
            throw new System.NotImplementedException();
        }

        public Vehicle GetVehicle(string plate)
        {
            return Vehicles.Count > 0 ? Vehicles[Vehicles.Count - 1] : new Vehicle() { Make = "aa", Model = "bb", Plate = "cc", Year = 1 };
        }

        public void SaveVehicle(Vehicle vehicle)
        {
           Vehicles.Add(vehicle);
        }

        public void SaveCurrentPoint(newPlayerStatus point)
        {
            //LocalizationPoints.Add(point);
            LocalizationPoints.Add(new newPlayerStatus(23,44));
        }

        public List<newPlayerStatus> GetCurrentPoints(newPlayerStatus point)
        {
            return LocalizationPoints;
        }

        public List<GamesToSend> GetAllGames()
        {
            return dataConn.showAllGames();
        }

        public string ShowAll(string table)
        {
            return dataConn.showAll(table);
        }
        
        public string CreateGame(Game game)
        {
            string gameName = game.name;
            string max_players = game.max_players;
            string max_points = game.max_points;

            string id_game = dataConn.createGame(gameName, max_players, max_points);

            return id_game;
        }
        
        public string CreateArea(Area area)
        {
            string id_game = area.id_game;

            int ax = int.Parse(area.latitude_area_a);
            int ay = int.Parse(area.longitude_area_a);

            int bx = int.Parse(area.latitude_area_b);
            int by = int.Parse(area.longitude_area_b);

            int cx = int.Parse(area.latitude_area_c);
            int cy = int.Parse(area.longitude_area_c);

            int dx = int.Parse(area.latitude_area_d);
            int dy = int.Parse(area.longitude_area_d);

            dataConn.createArea(id_game,ax,ay,bx,by,cx,cy,dx,dy);
            return null;
        }

        public string CreateAreaTest()
        {
            dataConn.createArea("12345", 1,2,3,4,5,6,7,8);
            return "CreateAreaTest";
        }

        public string CreatePlayer(Player player)
        {
            string id_game = dataConn.getGameId(player.gameName);
            string name = player.name;
            bool team = bool.Parse(player.team);
            string latitude = player.latitude;
            string longitude = player.longitude;
            dataConn.createPlayer(id_game,name, team, latitude,longitude);
            return dataConn.getPlayerId(name);
        }

        public string CreatePlayerTest()
        {
            dataConn.createPlayer("1","test",false,"0","0");
            return null;
        }

        public Area GetGameArea(string gameName)
        {
            string id_game = dataConn.getGameId(gameName);
            return dataConn.getGameArea(id_game);
        }

        public List<PlayerStatus> getPlayerPoints(string playerID,string gameID,string latitude,string longitude)
        {
            dataConn.UpdataPosition(playerID, latitude, longitude);
            // logika gry
            bool outAofArea = gameLogic.action(playerID, gameID, latitude, longitude, dataConn);
            string points ;
            if(outAofArea)
                points = (-1).ToString();
            else
                points = dataConn.getPoints(gameID, playerID);
            List<PlayerStatus> ret = dataConn.getPlayers(playerID,gameID,points);
            
            return ret;
        }

        public string clearDataBase()
        {
            dataConn.clearDataBasa("admin");
            return null;
        }

        public string GetPoints(string id_game,string id_player)
        {
            return dataConn.getPoints(id_game,id_player);
        }
        #endregion
    }
}